import { Effect , Reducer, Subscription } from 'umi';
import {  geFindData} from "@/services";
import { commentItem } from '@/utils/comment';

export interface IndexModelState {
    name: string;
    commData : commentItem[];
}

export interface IndexModelType {
    namespace: 'comment';
    state: IndexModelState;
    effects: {
        getData : Effect;
        // getFin : Effect;
    };
    reducers: {
        save: Reducer<IndexModelState>;
        // 启用 immer 之后
        // save: ImmerReducer<IndexModelState>;
    };
    subscriptions?: { setup: Subscription };
}

const commentModel: IndexModelType = {
    namespace: 'comment',

    state: {
        name: '874532',
        commData : []
    },

    effects: {

        // 获取数据
        *getData({ payload } , { call , put }){
            
            let {params} = payload;
            console.log( 'dva_params...' , params);

            let result = yield geFindData({ params : { name : params.name , email : params.email , pass : params.pass }});
            yield put({
                type : 'save',
                payload :{
                    commData : result.data[0]
                }
            })
        }

    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
        // 启用 immer 之后
        // save(state, action) {
        //   state.name = action.payload;
        // },
    },

};

export default commentModel;